﻿namespace Website

open IntelliFactory.WebSharper
open IntelliFactory.WebSharper.Html
open IntelliFactory.WebSharper.JQuery
open IntelliFactory.WebSharper.JQuery.Mobile
open IntelliFactory.WebSharper.Html5

module MIScript =
    [<JavaScript>]
    let SupportsHTML5() = 
        let elem = Dom.Document.Current.CreateElement("canvas")
        let ctxProperty = JavaScript.Get "getContext"
        let ctx = ctxProperty(elem)
        ctx <> null
    
    [<JavaScript>]
    let Main() =
        JQuery.Mobile.Mobile.Use() // should trigger webresource.
        JQuery.Mobile.Mobile.Instance.PushStateEnabled <- false
        if not(SupportsHTML5()) then
            JavaScript.Alert ("This site requires a HTML5 capable browser. Your current browser does not seem to support HTML5")
            Div[Text "Use an HTML5 capable browser"]
        else
            let rootPage = Policies.PoliciesPage()
            let claimsPage = Claims.ClaimsPage()
            let notificationPage = SiteCommon.notificationPage()
            let dialogPage = SiteCommon.dialogPage()
            let claimDetail = Claims.ClaimsDetailPage()
            let newClaim = ClaimInput.ClaimIputPage()
            let billingPage = Billing.BillingPage()
            let contactusPage = ContactUs.ContactUsPage()
            let pages =
                [
                    rootPage
                    dialogPage
                    notificationPage
                    claimsPage
                    claimDetail
                    newClaim
                    contactusPage
                    billingPage
                ]
            Div pages
            |>! OnAfterRender (fun _ -> 

                pages 
                |> List.iter (fun elt -> 
                    JQuery.Of(elt.Body) 
                    |> JQuery.Mobile.JQuery.Page 
                    |> ignore)

                JQuery.Of(rootPage.Body) 
                |> JQuery.Mobile.Mobile.Instance.ChangePage
                )

type MobileInsuranceViewer() =
    inherit Web.Control()

    [<JavaScript>]
    override this.Body = MIScript.Main() :> _
